<template>
  <div>
    <SearchForm
      :formInline="formInline"
      @handleSearchFormValue="handleSearchFormValue"
      @handleReset="handleReset"
    />
    <el-button type="primary" @click="handleDialogFormVisible"
      >新增数据</el-button
    >
    <el-table :data="list" style="width: 100%; height: 500px">
      <el-table-column fixed prop="id" label="id" />
      <el-table-column prop="name" label="小区名称" />
      <el-table-column prop="title" label="标题" />
      <el-table-column prop="person" label="经纪人" />
      <el-table-column prop="state" label="是否售出">
        <template #default="{ row }">
          <span>{{ row.state === "1" ? "已出售" : "未出售" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="size" label="房间大小" />
      <el-table-column prop="sizeRoom" label="房间" />
      <el-table-column prop="level" label="装修" />
      <el-table-column prop="price" label="房间价格" />
      <el-table-column prop="url" label="图片">
        <template #default="{ row }">
          <el-image
            style="width: 100px; height: 100px"
            :src="row.url"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="[row.url]"
            :initial-index="4"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleDialogFormVisible(row)"
            >编辑</el-button
          >
          <el-button link type="danger" @click="handleDel(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageCode"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :small="small"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    />
    <el-dialog v-model="dialogFormVisible" :title="dialogTitle">
      <el-form :model="dialogForm">
        <el-form-item label="小区id：" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.id" disabled autocomplete="off" />
        </el-form-item>
        <el-form-item label="小区名称：" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="标题：" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="房间大小：" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.size" autocomplete="off" />
        </el-form-item>
        <el-form-item label="图片：" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.url" autocomplete="off" />
        </el-form-item>
        <el-form-item label="房间价格：" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.price" autocomplete="off" />
        </el-form-item>
        <el-form-item label="是否出售：" :label-width="formLabelWidth">
          <el-select
            v-model="dialogForm.state"
            placeholder="请选择小区是否出售"
          >
            <el-option label="已经出售" value="1" />
            <el-option label="未出售" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="经纪人：" :label-width="formLabelWidth">
          <el-select v-model="dialogForm.person" placeholder="请选择小区经纪人">
            <el-option label="李兰" value="李兰" />
            <el-option label="黄明" value="黄明" />
            <el-option label="昭武" value="昭武" />
            <el-option label="李四" value="李四" />
          </el-select>
        </el-form-item>
        <el-form-item label="装修：" :label-width="formLabelWidth">
          <el-select v-model="dialogForm.level" placeholder="请选择装修">
            <el-option label="毛坯" value="毛坯" />
            <el-option label="精装" value="精装" />
            <el-option label="简装" value="简装" />
          </el-select>
        </el-form-item>
        <el-form-item label="房间：" :label-width="formLabelWidth">
          <el-select v-model="dialogForm.sizeRoom" placeholder="请选择房屋类型">
            <el-option label="一居" value="一居" />
            <el-option label="二居" value="二居" />
            <el-option label="三居" value="三居" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleDialogCancel">取消</el-button>
          <el-button type="primary" @click="handleDialogSubmit">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import request from "@/utils/request";
import { ref, watch } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import SearchForm from "./searchForm.vue";

const list = ref([]);
const total = ref(0);
const pageCode = ref(1);
const pageSize = ref(10);

const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const dialogTitle = ref("新增");
const dialogForm = ref({});

const formInline = ref({
  id: "",
  person: "",
});

// 搜索
const handleSearchFormValue = (val) => {
  formInline.value = {
    id: val.id,
    person: val.person,
  };
  getData();
};

// 重置
const handleReset = () => {
  formInline.value = {
    id: "",
    person: "",
  };
  getData();
};

const getData = () => {
  request
    .get("/room/list", {
      params: {
        pageCode: pageCode.value,
        pageSize: pageSize.value,
        ...formInline.value,
      },
    })
    .then((res) => {
      if (res.status === 200 && res.data.code === 10000) {
        list.value = res.data.data.list;
        total.value = res.data.data.total;
      }
    });
};
getData();

// 点击新增、编辑按钮打开弹框并且修改弹框标题
const handleDialogFormVisible = (row) => {
  dialogFormVisible.value = true;
  if (row.id) {
    dialogTitle.value = "编辑";
    dialogForm.value = { ...row };
  } else {
    dialogTitle.value = "新增";
  }
};

const handleDialogCancel = () => {
  dialogFormVisible.value = false;
  dialogForm.value = {};
};

// 提交表单
const handleDialogSubmit = async () => {
  if (dialogForm.value.id) {
    const res = await request.post("/room/list/edit", dialogForm.value);
    if (res.status === 200 && res.data.code === 0) {
      ElMessage({
        message: res.data.message,
        type: "success",
      });
    } else {
      ElMessage({
        message: res.data.message,
        type: "error",
      });
    }
  } else {
    const res = await request.put("/room/list", dialogForm.value);
    console.log(res.data, "===res");
    if (res.status === 200 && res.data.code === 10000) {
      ElMessage({
        message: res.data.message,
        type: "success",
      });
    } else {
      ElMessage({
        message: res.data.message,
        type: "error",
      });
    }
  }
  getData();
  dialogFormVisible.value = false;
  dialogForm.value = {};
};

// 删除
const handleDel = (row) => {
  ElMessageBox.confirm("你确定要删除嘛?", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    request.delete("/room/list", { params: { id: row.id } }).then((res) => {
      if (res.status === 200 && res.data.code === 10000) {
        ElMessage({
          message: res.data.message,
          type: "success",
        });
        getData();
      } else {
        ElMessage({
          message: res.data.message,
          type: "error",
        });
      }
    });
  });
  // .catch(() => {
  //   ElMessage({
  //     type: "info",
  //     message: "取消删除",
  //   });
  // });
};

watch([pageCode, pageSize], () => {
  getData();
});
</script>